package com.gr.smartedubackend.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gr.smartedubackend.common.BaseResponse;
import com.gr.smartedubackend.common.DeleteRequest;
import com.gr.smartedubackend.common.ErrorCode;
import com.gr.smartedubackend.model.dto.student_score.StudentScoreAddRequest;
import com.gr.smartedubackend.model.dto.student_score.StudentScoreQueryRequest;
import com.gr.smartedubackend.model.dto.student_score.StudentScoreUpdateRequest;
import com.gr.smartedubackend.model.entity.StudentScore;
import com.gr.smartedubackend.service.StudentScoreService;
import com.gr.smartedubackend.utils.ResultUtils;
import com.gr.smartedubackend.utils.ThrowUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/student_score")
public class StudentScoreController {

    @Resource
    private StudentScoreService studentScoreService;

    @PostMapping("/add")
    public BaseResponse<Long> addStudentScore(@RequestBody StudentScoreAddRequest studentScoreAddRequest) {
        ThrowUtils.throwIf(studentScoreAddRequest == null, ErrorCode.PARAMS_ERROR);
        StudentScore studentScore = new StudentScore();
        BeanUtils.copyProperties(studentScoreAddRequest, studentScore);
        Integer score = studentScore.getScore();
        if (score > 90 && score <= 100) {
            studentScore.setGradeLevel("A");
        } else if (score > 75 && score <= 90) {
            studentScore.setGradeLevel("B");
        } else if (score > 60 && score <= 75) {
            studentScore.setGradeLevel("C");
        } else {
            studentScore.setGradeLevel("D");
        }
        boolean result = studentScoreService.save(studentScore);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(studentScore.getId());
    }

    @GetMapping("/get")
    public BaseResponse<StudentScore> getStudentScoreById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        StudentScore studentScore = studentScoreService.getById(id);
        ThrowUtils.throwIf(studentScore == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(studentScore);
    }

    @GetMapping("/get/studentNum/byLevel")
    public BaseResponse<Map<String, Long>> getStudentNumByLevel(@RequestParam String courseName) {
        ThrowUtils.throwIf(StrUtil.isBlank(courseName), ErrorCode.PARAMS_ERROR);
        QueryWrapper<StudentScore> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("gradeLevel", "COUNT(id) AS num")
                .eq("courseName", courseName)
                .groupBy("gradeLevel");
        List<Map<String, Object>> list = studentScoreService.listMaps(queryWrapper);
        Map<String, Long> result = new HashMap<>();
        for (Map<String, Object> map : list) {
            result.put(map.get("gradeLevel").toString(), (Long) map.get("num"));
        }
        return ResultUtils.success(result);
    }

    @PostMapping("/list/page")
    public BaseResponse<Page<StudentScore>> listStudentScoreByPage(@RequestBody StudentScoreQueryRequest studentScoreQueryRequest) {
        ThrowUtils.throwIf(studentScoreQueryRequest == null, ErrorCode.PARAMS_ERROR);
        int current = studentScoreQueryRequest.getCurrent();
        int pageSize = studentScoreQueryRequest.getPageSize();
        Page<StudentScore> studentScorePage = studentScoreService.page(new Page<>(current, pageSize),
                studentScoreService.getQueryWrapper(studentScoreQueryRequest));
        return ResultUtils.success(studentScorePage);
    }

    @PostMapping("/list/page/student")
    public BaseResponse<Page<StudentScore>> listScorePageByStudentId(@RequestBody StudentScoreQueryRequest studentScoreQueryRequest) {
        ThrowUtils.throwIf(studentScoreQueryRequest == null, ErrorCode.PARAMS_ERROR);
        int current = studentScoreQueryRequest.getCurrent();
        int pageSize = studentScoreQueryRequest.getPageSize();
        Page<StudentScore> studentScorePage = studentScoreService.page(new Page<>(current, pageSize),
                studentScoreService.getQueryWrapper(studentScoreQueryRequest));
        return ResultUtils.success(studentScorePage);
    }


    @PostMapping("/list")
    public BaseResponse<List<StudentScore>> listStudentScore(@RequestBody StudentScoreQueryRequest studentScoreQueryRequest) {
        ThrowUtils.throwIf(studentScoreQueryRequest == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(studentScoreService.list(studentScoreService.getQueryWrapper(studentScoreQueryRequest)));
    }


    @PostMapping("/update")
    public BaseResponse<Boolean> updateStudentScore(@RequestBody StudentScoreUpdateRequest studentScoreUpdateRequest) {
        ThrowUtils.throwIf(studentScoreUpdateRequest == null || studentScoreUpdateRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        StudentScore studentScore = new StudentScore();
        BeanUtils.copyProperties(studentScoreUpdateRequest, studentScore);
        Integer score = studentScore.getScore();
        if (score > 90 && score <= 100) {
            studentScore.setGradeLevel("A");
        } else if (score > 75 && score <= 90) {
            studentScore.setGradeLevel("B");
        } else if (score > 60 && score <= 75) {
            studentScore.setGradeLevel("C");
        } else {
            studentScore.setGradeLevel("D");
        }
        boolean result = studentScoreService.updateById(studentScore);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteStudentScore(@RequestBody DeleteRequest deleteRequest) {
        ThrowUtils.throwIf(deleteRequest == null || deleteRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        boolean result = studentScoreService.removeById(deleteRequest.getId());
        return ResultUtils.success(result);
    }
}
